package com.nuoniu.sibanyun.controller.finance;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.finance.VRuleVoucherSetMain;
import com.nuoniu.sibanyun.service.finance.IVRuleVoucherSetMainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 规则凭证 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2022-05-06
 */
@RestController
@RequestMapping("/ruleVoucher")
@Api(tags = "规则凭证管理")
public class VRuleVoucherSetMainController extends BaseController {

    @Autowired
    private IVRuleVoucherSetMainService  ruleVoucherSetMainService;

    @ApiOperation("获取列表")
    @GetMapping("pageList")
    public HttpResult pageList(Page page, VRuleVoucherSetMain ruleVoucherSetMain) {
        return HttpResult.ok(ruleVoucherSetMainService.pageList(page, ruleVoucherSetMain));
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody VRuleVoucherSetMain ruleVoucherSetMain) {
        if(ruleVoucherSetMain == null || StringUtils.isBlank(ruleVoucherSetMain.getCode())) {
            return HttpResult.error("编码不能为空");
        }
        if(ruleVoucherSetMain == null || StringUtils.isBlank(ruleVoucherSetMain.getName())) {
            return HttpResult.error("名称不能为空");
        }
        if(ruleVoucherSetMain.getOrderDetailList().size() == 0) {
            return HttpResult.error("明细不能为空");
        }

        ruleVoucherSetMainService.saveRule(ruleVoucherSetMain);
        return HttpResult.ok();
    }

    @ApiOperation("获取模版详情")
    @GetMapping("getDetail")
    public HttpResult getDetail(Long id) {
        return HttpResult.ok(ruleVoucherSetMainService.getDetail(id));
    }

    @ApiOperation("删除模版")
    @DeleteMapping("deleteById")
        public HttpResult deleteById(Long id) {
        ruleVoucherSetMainService.deleteById(id);
        return HttpResult.ok();
    }
}
